package com.budwk.app.sqfwDoc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.budwk.app.sqfwDoc.models.SqfwTemplateVisDict;
import com.budwk.app.sqfwDoc.service.SqfwTemplateFieldService;
import com.budwk.app.sqfwDoc.service.SqfwTemplateVisDictService;
import com.budwk.app.sqfwDoc.vo.SqfwTemplateVisDictSimpleVO;
import com.budwk.app.sqfwDoc.vo.SqfwTemplateVisDictTreeVO;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.List;
import java.util.Map;

/**
 * @author ZY.Wang
 * @since 2023/12/11
 */
@IocBean(args = {"refer:dao"})
public class SqfwTemplateVisServiceImpl extends BaseServiceImpl<SqfwTemplateVisDict> implements SqfwTemplateVisDictService {
    public SqfwTemplateVisServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private SqfwTemplateFieldService sqfwTemplateFieldService;

    @Override
    public List<SqfwTemplateVisDictTreeVO> listVisDictTree() {
        List<SqfwTemplateVisDictTreeVO> resultList = Lists.newArrayList();

        Map<String, List<SqfwTemplateVisDict>> preAllMap = getAllSimpleMap();
        for (SqfwTemplateVisDictTreeVO parentVidDict : getRootNode(preAllMap)) {
            List<SqfwTemplateVisDictTreeVO> childrenVisDict = buildChildren(parentVidDict, preAllMap);
            parentVidDict.setExpanded(childrenVisDict.size() > 0);
            parentVidDict.setChildren(childrenVisDict);
            resultList.add(parentVidDict);
        }

        return resultList;
    }

    @Override
    public SqfwTemplateVisDictSimpleVO getDetail(String id) {
        SqfwTemplateVisDict dict = fetch(id);
        if (dict == null) return null;

        SqfwTemplateVisDictSimpleVO vo = new SqfwTemplateVisDictSimpleVO();

        String parentId = dict.getParentId();
        String templateDictId = dict.getTemplateDictId();
        SqfwTemplateVisDict parentVisDict = fetch(parentId);

        vo.setParentName(parentVisDict.getVisDictName());
        vo.setTemplateDict(sqfwTemplateFieldService.fetch(templateDictId));

        return vo;
    }

    /**
     * 创建子字典
     *
     * @param parentVidDict 字段
     * @param preAllMap     预制的map
     * @return 结果
     */
    private List<SqfwTemplateVisDictTreeVO> buildChildren(SqfwTemplateVisDictTreeVO parentVidDict, Map<String, List<SqfwTemplateVisDict>> preAllMap) {
        List<SqfwTemplateVisDictTreeVO> childrenList = Lists.newArrayList();

        List<SqfwTemplateVisDict> dictList = preAllMap.get(parentVidDict.getId());
        if (dictList != null) {
            for (SqfwTemplateVisDict visDict : dictList) {
                if (visDict.getParentId().equals(parentVidDict.getId())) {
                    SqfwTemplateVisDictTreeVO vo = new SqfwTemplateVisDictTreeVO();
                    BeanUtil.copyProperties(visDict, vo);

                    List<SqfwTemplateVisDictTreeVO> children = buildChildren(vo, preAllMap);
                    vo.setChildren(children);
                    vo.setExpanded(children.size() > 0);
                    childrenList.add(vo);
                }
            }
        }
        return childrenList;
    }

    /**
     * 获取根节点
     *
     * @param preAllMap 预先制作的map
     * @return 结果
     */
    private List<SqfwTemplateVisDictTreeVO> getRootNode(Map<String, List<SqfwTemplateVisDict>> preAllMap) {
        List<SqfwTemplateVisDictTreeVO> rootList = Lists.newArrayList();
        List<SqfwTemplateVisDict> dictList = preAllMap.get("0");
        if (dictList == null || dictList.size() == 0) return rootList;
        for (SqfwTemplateVisDict visDict : dictList) {
            String parentId = visDict.getParentId();
            if (parentId.equals("0")) {
                SqfwTemplateVisDictTreeVO vo = new SqfwTemplateVisDictTreeVO();
                BeanUtil.copyProperties(visDict, vo);
                rootList.add(vo);
            }
        }
        return rootList;
    }

    /**
     * 将所有的字典预制成map
     *
     * @return 结果
     */
    private Map<String, List<SqfwTemplateVisDict>> getAllSimpleMap() {
        List<SqfwTemplateVisDict> allList = query();

        Map<String, List<SqfwTemplateVisDict>> resultMap = Maps.newHashMap();
        for (SqfwTemplateVisDict visDict : allList) {
            String parentId = visDict.getParentId();
            if (StringUtils.isBlank(parentId)) parentId = "0";

            if (resultMap.containsKey(parentId)) {
                resultMap.get(parentId).add(visDict);
            } else {
                resultMap.put(parentId, Lists.newArrayList(visDict));
            }
        }
        return resultMap;
    }
}
